/**
 * Copyright 2006 Brad Reynolds
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.leeloo.databinding.internal.ui;

import org.eclipse.jdt.debug.core.IJavaObject;
import org.eclipse.jdt.debug.core.IJavaStackFrame;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.widgets.Event;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IViewReference;
import org.leeloo.core.LeelooPlugin;
import org.leeloo.core.Tag;
import org.leeloo.core.TagService;
import org.leeloo.core.Tags;
import org.leeloo.databinding.ui.TagFilterManager;
import org.leeloo.databinding.ui.view.EventLog;
import org.leeloo.internal.core.AbstractVariablesViewActionDelegate;
import org.leeloo.internal.core.JavaObjects;
import org.leeloo.internal.core.ObjectDescriptor;

/**
 * Action that will filter the Databinding Log for the selected
 * <code>Binding</code> in the view. If the selected item does not participate
 * in databinding the action will be disabled.
 * 
 * @author Brad Reynolds
 */

// TODO change the disabling to using IActionFilter
public class FilterBindingAction extends AbstractVariablesViewActionDelegate {
    /**
     * Host view of the action.
     */
    private IViewPart hostView;

    private IJavaStackFrame stackFrame;

    private IJavaObject javaObject;

    /**
     * Store the view the action is bound to.
     * 
     * @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart)
     */
    public void init(IViewPart view) {
        this.hostView = view;
    }

    /**
     * Disable the action initially.
     * 
     * @see org.eclipse.ui.actions.ActionDelegate#init(org.eclipse.jface.action.IAction)
     */
    public void init(IAction action) {
        action.setEnabled(false);
    }

    /**
     * Enables the item if the selected item in the view is an instance of
     * Binding.class.
     * 
     * @see org.eclipse.ui.actions.ActionDelegate#selectionChanged(org.eclipse.jface.action.IAction,
     *      org.eclipse.jface.viewers.ISelection)
     */
    public void selectionChanged(IAction action, ISelection selection) {
        this.javaObject = null;
        this.stackFrame = null;
        action.setEnabled(false);

        javaObject = getJavaObject(selection);

        if (javaObject != null) {
            stackFrame = getStackFrame(hostView);
            if (stackFrame != null) {
                ObjectDescriptor descriptor = JavaObjects.getObjectDescriptor(javaObject, stackFrame);

                TagService tagService = LeelooPlugin.getDefault().getTagService();
                if (tagService.contains(descriptor, Databinding.DATABINDING)) {
                    action.setEnabled(true);
                }
            }
        }
    }

    /**
     * Creates a filter and sets it as the sole selected filter for the log
     * view.
     * 
     * @see org.eclipse.ui.actions.ActionDelegate#runWithEvent(org.eclipse.jface.action.IAction,
     *      org.eclipse.swt.widgets.Event)
     */
    public void runWithEvent(IAction action, Event event) {
        assert stackFrame != null : "stackFrame == null"; //$NON-NLS-1$
        assert javaObject != null : "javaObject == null"; //$NON-NLS-1$

        IViewReference[] references = hostView.getViewSite().getWorkbenchWindow().getActivePage().getViewReferences();
        EventLog logView = null;
        for (int i = 0; logView == null && i < references.length; i++) {
            if (references[i].getView(false) instanceof EventLog) {
                logView = (EventLog) references[i].getView(true);
            }
        }

        if (logView != null) {
            ObjectDescriptor descriptor = JavaObjects.getObjectDescriptor(javaObject, stackFrame);
            TagService tagService = LeelooPlugin.getDefault().getTagService();
            Tag[] tags = tagService.getAll(descriptor);
            for (int i = 0; i < tags.length; i++) {
                Tag tag = tags[i];
                if (Tags.ID.equals(tag.getName())) {
                    TagFilterManager manager = logView.getTagFilterManager();
                    manager.addTag(tag);
                    manager.setSelection(new Tag[] { tag });
                    break;
                }
            }
        }
    }
}